//
//  RedRecordListViewController.m
//  智积分
//
//  Created by Cynic on 2018/7/31.
//  Copyright © 2018年 Cynic. All rights reserved.
//

#import "RedRecordListViewController.h"
#import "RedRecordTableViewCell.h"
#import "RedBagDetailInfiViewController.h"

static NSString *RedRecordTableViewCellIdentifier = @"RedRecordTableViewCellIdentifier";
@interface RedRecordListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,retain)UITableView *MainView;
@property (nonatomic ,assign)NSInteger currentPage;
@property (nonatomic ,retain)NSMutableArray *resultData;
@end

@implementation RedRecordListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = bggrayColor;
    _resultData = [NSMutableArray array];
    _currentPage = 0;
    [self viewLoad];
    // Do any additional setup after loading the view.
}

-(void)viewLoad{
    [self setNavigationViewWithTitle:@"红包记录" WithCanPop:YES WithPopData:@"return" WithPopisString:NO WithHasRightOperation:NO WithrightOperationData:@"" WithRightisString:NO WithNavigationColor:[UIColor ColorWithHex:@"DF4432" withAlpha:1] withBgView:self.view];
    [self.view addSubview:self.MainView];
    [self DataLoadMore:NO];
}

-(void)DataLoadMore:(BOOL)more{
    if (more) {
        _currentPage ++;
    }
    else{
        _resultData = [NSMutableArray array];
        _currentPage = 1;
    }
    NetWorkModel *netmodel = [[NetWorkModel alloc] init];
    [SVProgressHUD showWithStatus:@"加载中"];
    [netmodel getSellerSendRedBagHisByPage:_currentPage withShopID:cxsingler.loginrequest[@"admin"][@"shop_id"]];//
    WEAKSELF;
    netmodel.sendMessage = ^(NSString *result) {
        if ([weakSelf.MainView.mj_header isRefreshing]) {
            [weakSelf.MainView.mj_header endRefreshing];
        }
        else if ([weakSelf.MainView.mj_footer isRefreshing]){
            [weakSelf.MainView.mj_footer endRefreshing];
        }
        if ([result isEqual:@"success"]) {
            [SVProgressHUD dismiss];
            NSArray *list = cxsingler.ArraryData;
            if (list.count > 0) {
                [_resultData addObjectsFromArray:list];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf.MainView reloadData];
                });
            }
        }
        else{
            [SVProgressHUD showErrorWithStatus:result];
        }
    };
}

-(UITableView *)MainView{
    if (!_MainView) {
        _MainView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44 + cxsingler.topdis, cx_wid, cx_hit - 44 - cxsingler.topdis - cxsingler.bottomdis)];
        _MainView.delegate = self;
        _MainView.dataSource = self;
        _MainView.estimatedRowHeight = 44;
        _MainView.backgroundColor = bggrayColor;
        _MainView.rowHeight = UITableViewAutomaticDimension;
        _MainView.separatorStyle = UITableViewCellSelectionStyleNone;
        WEAKSELF;
        MJRefreshGifHeader *Gifheader = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            [weakSelf DataLoadMore:NO];
        }];
        MJRefreshBackGifFooter *footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
            [weakSelf DataLoadMore:YES];
        }];
        [Gifheader setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
        [Gifheader setTitle:@"松开刷新" forState:MJRefreshStatePulling];
        [Gifheader setTitle:@"刷新中……" forState:MJRefreshStateRefreshing];
        [footer setTitle:@"上拉加载更多" forState:MJRefreshStateIdle];
        [footer setTitle:@"松开加载更多" forState:MJRefreshStatePulling];
        [footer setTitle:@"加载更多中……" forState:MJRefreshStateRefreshing];
        _MainView.mj_header = Gifheader;
        _MainView.mj_footer = footer;
        [_MainView registerClass:[RedRecordTableViewCell class] forCellReuseIdentifier:RedRecordTableViewCellIdentifier];
    }
    return _MainView;
}

#pragma mark--uitbaleview代理start
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _resultData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    RedRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RedRecordTableViewCellIdentifier];
    NSDictionary *partDic = _resultData[row];
    [cell.totalCentBtn setTitle:[NSString stringWithFormat:@"%@",partDic[@"integral"]].MakeIntStr forState:UIControlStateNormal];
    [cell.totalRedNumBtn setTitle:[NSString stringWithFormat:@"%@",partDic[@"count"]] forState:UIControlStateNormal];
    [cell.totalRedGetBtn setTitle:[NSString stringWithFormat:@"%@",partDic[@"record_count"]].MakeIntStr forState:UIControlStateNormal];;
    cell.time.text = [self gettimeByInter:[partDic[@"update_time"] integerValue]];
    cell.backgroundColor = bggrayColor;
//    __weak typeof(cell) weakCell = cell;
    [cell.detailBtn cx_eventsBlockHandler:^{
        RedBagDetailInfiViewController *nextVC = [[RedBagDetailInfiViewController alloc] initWithID:partDic[@"id"]];
        [self.navigationController pushViewController:nextVC animated:YES];
    } forControlEvents:UIControlEventTouchUpInside];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark--uitableview代理end

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
